import axios from 'axios';
import qs from "qs";

// 登录请求方法
const loginreq = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/json',
        },
        data: params,
        traditional: true
    }).then(res => res.data);
};
// 通用公用方法
const req = (method, url, params = null) => {
    if (method === 'get') {
        return axios({
            method: method,
            url: `${url}?${qs.stringify(params)}`,
            headers: {
                'Content-Type': 'application/json',
                token: localStorage.getItem('logintoken')
            },
            data: params,
            traditional: true
        }).then(res => res.data);
    }
    if (method === 'post') {
        return axios({
            method: method,
            url: url,
            headers: {
                'Content-Type': 'application/json',
                token: localStorage.getItem('logintoken')
            },
            data: params,
            traditional: true
        }).then(res => res.data);
    }
};
// 上传图片
const reqImg = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'multipart/form-data',
            token: localStorage.getItem('logintoken')
        },
        data: params,
        traditional: true
    }).then(res => res.data);
};

export {
    loginreq,
    req,
    reqImg
}
